package com.dartfar.menu.service;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.beanutils.BeanUtils;
import org.hibernate.type.IntegerType;
import org.hibernate.type.StringType;
import org.hibernate.type.Type;
import org.springframework.stereotype.Component;

import com.dartfar.base.dao.DaoUtil;
import com.dartfar.base.model.Role;
import com.dartfar.base.service.RoleService;
import com.dartfar.menu.model.Menu;
import com.dartfar.menu.vo.MenuVo;


@Component("menuService")
public class MenuServiceImpl implements MenuService {
	
	@Resource
	private DaoUtil<Menu> menuDaoUtil;
	@Resource
	private RoleService roleService;

	/**
	 * 添加功能
	 * @param menu 功能对象
	 * @return 返回添加功能的ID
	 */
	@Override
	public int add(Menu menu) {
		return menuDaoUtil.save(menu);
	}
	

	@Override
	public int add(MenuVo menuVo) {
		// TODO Auto-generated method stub
		//获取父功能
		Menu mainMenu = get(menuVo.getMainId());
		Menu parentMenu = get(menuVo.getParentId());
		
		Menu menu = new Menu();
		menu.setLeaf(true);
		menu.setMainMenu(mainMenu);
		menu.setParentMenu(parentMenu);
		//menuVo对象克隆menu
		try{
			BeanUtils.copyProperties(menu, menuVo);
		}catch(Exception ex){
			ex.printStackTrace();
		}
		
		if(mainMenu != null){
			mainMenu.setLeaf(false);
			menuDaoUtil.update(mainMenu);
		}
		
		//如果父功能不为空，则设置父功能的叶子结点状态为否。
		if(parentMenu != null){
			parentMenu.setLeaf(false);
			menuDaoUtil.update(parentMenu);
		}
		return menuDaoUtil.save(menu);
	}
	
	/**
	 * 获取所有功能的列表
	 * @return 所有功能的列表
	 */
	@Override
	public List<Menu> getList() {
		// TODO Auto-generated method stub
		return menuDaoUtil.getList("from Menu order by m.menuOrder asc");
	}
	
	/**
	 * 获取主菜单列表
	 * @return 返回菜单列表
	 */
	@Override
	public List<Menu> getMainMenuLsit(){
			return menuDaoUtil.getList("from Menu m where m.mainMenu is null order by m.menuOrder asc");
	}
	
	/**
	 * 根据父ID和模块ID回去功能列表
	 * @param parentId 父ID
	 * @param moduleId 模块ID
	 * @return 返回功能列表
	 */
	@Override
	public List<Menu> getListByParentId(int parentId, int mainId){
		if(parentId == 0){
			return menuDaoUtil.getList("from Menu m where m.parentMenu is null and m.mainMenu.id = ? order by m.menuOrder asc", new Object[]{mainId},new Type[]{new IntegerType()});
		}else{
			return menuDaoUtil.getList("from Menu m where m.parentMenu.id = ? and m.mainMenu.id = ? order by m.menuOrder asc",new Object[]{parentId, mainId},new Type[]{new IntegerType(), new IntegerType()});
		}
	}
	
	/**
	 * 获取子菜单列表
	 * @return 返回菜单列表
	 */
	@Override
	public List<Menu> getSubMenuList(int mainId){
		if(mainId == 0){
			return menuDaoUtil.getList("from Menu m where m.mainMenu is not null order by m.menuOrder asc");
		}else{
			return menuDaoUtil.getList("from Menu m where m.mainMenu.id = ? order by m.menuOrder asc", new Object[]{mainId},new Type[]{new IntegerType()});
		}
			
	}

	@Override
	public void modify(MenuVo menuVo) {
		// TODO Auto-generated method stub
		Menu menu = get(menuVo.getId());
		Menu parentMenu = get(menuVo.getParentId());
		Menu mainMenu = get(menuVo.getMainId());
		menu.setLeaf(true);
		menu.setMainMenu(mainMenu);
		menu.setParentMenu(parentMenu);

		try{ 
			BeanUtils.copyProperties(menu, menuVo);
		}catch(Exception ex){
			ex.printStackTrace();
		}
		menuDaoUtil.update(menu);
	}

	@Override
	public void delete(int id) {
		
		int parentId = 0;
		int mainId = 0;

		Menu menu = get(id);
		//获取父功能
		if(menu.getMainMenu() != null){
			mainId = menu.getMainMenu().getId();
		}
		if(menu.getParentMenu() != null){
			parentId = menu.getParentMenu().getId();
		}
			
		//删除菜单
		menuDaoUtil.delete(menu);
		
		List<Menu> list = getListByParentId(parentId, mainId);

		//如果没有子菜单
		if(list.size() == 0){
			//如果父菜单ID = 0 主菜单ID = 0 则为主菜单
			if(parentId == 0 && mainId != 0){
				menu = get(mainId);
				menu.setLeaf(true);
				menuDaoUtil.update(menu);
			}else if(parentId != 0 && mainId != 0){
				menu = get(parentId);
				menu.setLeaf(true);
				menuDaoUtil.update(menu);
			}
		}
		
		//删除相应的权限
		menuDaoUtil.execute("delete Authority a where a.menu.id = " + id);
		
	}
	
	/**
	 * 判断某主菜单中是否有子菜单
	 * @param id 模块的ID
	 * @return 子菜单中是否存在功能
	 */
	@Override
	public boolean existSubMenuByMenuId(int id) {
		long count = menuDaoUtil.getCount("from Menu m where m.mainMenu.id = ?", new Object[]{id},new Type[]{new IntegerType()});
		if(count == 0){
			return false;
		}else{
			return true;
		}
	}

	/**
	 * 根据角色权限获取功能方法
	 * @param role 角色对象
	 */
	@Override
	public List<Menu> getMenuByRoleId(int roleId) {
		Role role = roleService.get(roleId);
		///List<Menu> subMenu = role.getMenus();
		List<Menu> menus = new ArrayList<Menu>();
/*		for(Iterator<Menu> i = subMenu.iterator();i.hasNext();){
			Menu menu = i.next();
			menus.add(menu.getMainMenu());
		}*/
		return menus;
	}

	/**
	 * 根据多个功能的ID获取功能列表 
	 * @param functionIds 多个功能的ID 格式:1,2,3 
	 * @return 返回功能列表 
	 */
	@Override
	public List<Menu> getListByMenuIds(String menus) {
		return menuDaoUtil.getList("from Menu m where m.id in ("+ menus +")");
	}
	
	
	/**
	 * 根据功能的ID获取功能
	 * @param id 功能的ID
	 * @return 功能对象
	 */
	@Override
	public Menu get(int id) {
		return menuDaoUtil.get(id, Menu.class);	
	}
	


	@Override
	public void modify(Menu menu) {
		menuDaoUtil.update(menu);
	}

	/**
	 * 根据目标获取功能对象
	 * @param string 链接地址
	 */
	@Override
	public Menu getByTarget(String target) {
		List<Menu> menuList = menuDaoUtil.getList("from Menu m where m.target = ?", new Object[]{target},new Type[]{new StringType()});
		if(menuList.size() != 0){
			return menuList.get(0);
		}else{
			return null;
		}
	}
	
	@Override
	public void batchDeleteByModuleId(int id) {
		// TODO Auto-generated method stub
		
	}

}
